package com.example.mobile1921605093zlj0604;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

//创建服务：定义一个Sevice的子类
public class MusicSevice extends Service {
    MediaPlayer mediaPlayer;

    class ServiceBinder extends Binder {
        public MusicSevice getMusiceService() {
            return MusicSevice.this;
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.i("TAG", "onBind: ");
        return new ServiceBinder();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("TAG", "onCreate: ");
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i("TAG", "onUnbind: ");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        Log.i("TAG", "onDestroy: ");
        super.onDestroy();
        stopMusic();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("TAG", "onStartCommand: "+add(1,1));
        playMusic();
        return super.onStartCommand(intent, flags, startId);

    }
    public int add(int a,int b){
        return a+b;
    }
    public  void playMusic(){

            mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.old_boy);
        mediaPlayer.start();
    }
    public  void pauseMusic(){
        if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
            mediaPlayer.pause();
        }
    }
    public  void stopMusic(){
        if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer=null;
        }
    }
    public  void rePlay(){
        if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
            mediaPlayer.seekTo(0);
            mediaPlayer.start();
        }
    }
}


